/***************************************************************************
创建者：华磊
开始时间：               : 2019.12.13
copyright            : (C) 深圳市华友高科有限公司
 ***************************************************************************/
#include "belttrackview.h"
#include "ui_belttrackview.h"
#include "../CommunicateDelegate/communicatedelegate.h"
#include <QMessageBox>
#include <QStandardItemModel>

BeltTrackView::BeltTrackView(int styleId,CommunicateNormalDelegate *communicateNormalThreadIn,GlobalData *globalDataIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BeltTrackView)
{
    communicateNormalThread=communicateNormalThreadIn;
    globalData=globalDataIn;
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setStyleSheet("background-color:white;");
    //    setGeometry(70,128,460,595);
    move(0,0);
    initialDisplayStyle(styleId);
    hide();

    //视觉初始
//    for(int i=0;i<10;i++)
//    {
//          ui->comboBox_cameraProtocol->addItem(QString::number(i));
//    }
    ui->comboBox_cameraProtocol->addItem("无");
    ui->comboBox_cameraProtocol->addItem("3P");
    ui->comboBox_cameraProtocol->addItem("KEBA");
    ui->comboBox_isTrack->addItem("否");
    ui->comboBox_isTrack->addItem("是");
    ui->comboBox_isWorkZoneCoindence->addItem("否");
    ui->comboBox_isWorkZoneCoindence->addItem("是");
    ui->comboBox_trigMethod->addItem("定时");
    ui->comboBox_trigMethod->addItem("距离");
    ui->comboBox_trigMethod->addItem("开关触发偏移距离");
    ui->comboBox_trigMethod->addItem("开关触发自动距离");
    ui->comboBox_trigMethod->addItem("用户指令");
    ui->comboBox_trigMethod->addItem("开关触发物体距离+过滤");

    //跟踪初始
    for(int i=0;i<10;i++)
    {
          ui->comboBox_currenttrackIndex->addItem(QString::number(i));
    }
    ui->comboBox_trackFunction->addItem("不跟踪");
    ui->comboBox_trackFunction->addItem("常规跟踪");
    ui->comboBox_encoderDirection->addItem("正向");
    ui->comboBox_encoderDirection->addItem("反向");

    ui->comboBox_currentEncoderReceiver->addItem("０#编码器接收模块");
    ui->comboBox_currentEncoderReceiver->addItem("１#编码器接收模块");
    ui->comboBox_currentEncoderReceiver->addItem("２#编码器接收模块");
    ui->comboBox_currentEncoderReceiver->addItem("３#编码器接收模块");
    ui->comboBox_currentEncoderReceiver->addItem("４#编码器接收模块");
    ui->comboBox_currentEncoderReceiver->addItem("５#编码器接收模块");

    model_encoderReceiverInfo= new QStandardItemModel();
    model_beltTrackDebugInfo= new QStandardItemModel();

    timer=new QTimer;
    connect( timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );


}

void BeltTrackView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->pushButton_return->setGeometry(500,400,90,30);

        ui->scrollArea->setGeometry(10,0,480,440);

        ui->pushButton_connectCamera->setGeometry(270,15,160,30);
        ui->pushButton_forceTrigCamera->setGeometry(270,80,160,30);

        break;
    default:
        break;
    }
}

BeltTrackView::~BeltTrackView()
{
    delete ui;
}

void BeltTrackView::showView_slot()
{
    show();
    getVisionConfig_Info();
    getTrackConfig_Info();
    timer->start(300);
}

void BeltTrackView::on_pushButton_return_clicked()
{
    hide();
    timer->stop();
}

void BeltTrackView::getVisionConfig_Info()
{
    visionConfigList.clear();
    communicateNormalThread->getVisionTrackConfigList(visionConfigList);

    ui->comboBox_visionIndex->clear();
    for(int i=0;i<visionConfigList.size();i++)
    {
        ui->comboBox_visionIndex->addItem(QString::number(i));
    }

    if(visionConfigList.size()>0)
    {
           updateVisionConfigShow(0,visionConfigList[0]);
           QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("视觉配置获取成功"));
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("视觉配置为空"));
        return ;
    }


}

void BeltTrackView::on_pushButton_changeVisionConfig_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否修改视觉配置！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int id,returnFlag;
    returnFlag=-1;
    VisionTrackConfig visionConfig;

    id= ui->comboBox_visionIndex->currentIndex();
    visionConfig.cameraProtocol=ui->comboBox_cameraProtocol->currentIndex();
    visionConfig.isTrack=ui->comboBox_isTrack->currentIndex();
    visionConfig.cameraTrigMethod=ui->comboBox_trigMethod->currentIndex();
    visionConfig.trigTimeOrDistance=ui->lineEdit_trigValue->text().toDouble();
    visionConfig.beltTrackIndex=ui->lineEdit_beltTrackIndex->text().toInt();
    visionConfig.cameraIp=ui->lineEdit_cameraIp->text().toStdString();
    visionConfig.cameraPort=ui->lineEdit_cameraPort->text().toInt();
    visionConfig.cameraMmPerPix_x=ui->lineEdit_cameraMmPerPix_x->text().toDouble();
    visionConfig.cameraMmPerPix_y=ui->lineEdit_cameraMmPerPix_y->text().toDouble();
    visionConfig.cameraOffset_x=ui->lineEdit_cameraOffset_x->text().toDouble();
    visionConfig.cameraOffset_y=ui->lineEdit_cameraOffset_y->text().toDouble();
    visionConfig.cameraOffset_r=ui->lineEdit_cameraOffset_r->text().toDouble();
    visionConfig.cameraCommunicateTimeout=ui->lineEdit_cameraTimeOut->text().toDouble();
    visionConfig.objectMininumDistance=ui->lineEdit_minObjectDistance->text().toDouble();
    visionConfig.visionZoneEncoderRecorded=ui->lineEdit_visionZoneEncoderRecorded->text().toInt();
    visionConfig.robotZoneEncoderRecorded=ui->lineEdit_robotZoneEncoderRecorded->text().toInt();
    visionConfig.isWorkZoneCoincident=ui->comboBox_isWorkZoneCoindence->currentIndex();
    visionConfig.userCoordinateIndex=ui->lineEdit_visionUsercoordinateIndex->text().toInt();
    visionConfig.trigFilterRatio=ui->lineEdit_trigFilterRatio->text().toDouble();
    visionConfig.objectLength=ui->lineEdit_objectLength->text().toDouble();

    communicateNormalThread->changeVisionTrackConfig(id,visionConfig,returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("视觉配置修改成功"));
    }
    else
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("视觉配置修改失败"));
    }

}

void BeltTrackView::on_comboBox_visionIndex_currentIndexChanged(int index)
{

}

void BeltTrackView::updateVisionConfigShow(int visionIndex, VisionTrackConfig visionConfig)
{
        ui->comboBox_visionIndex->setCurrentIndex(visionIndex);
        ui->comboBox_cameraProtocol->setCurrentIndex(visionConfig.cameraProtocol);
        ui->comboBox_isTrack->setCurrentIndex(visionConfig.isTrack);
        ui->comboBox_trigMethod->setCurrentIndex(visionConfig.cameraTrigMethod);
        ui->lineEdit_trigValue->setText(QString::number(visionConfig.trigTimeOrDistance));
        ui->lineEdit_beltTrackIndex->setText(QString::number(visionConfig.beltTrackIndex));
        ui->lineEdit_cameraIp->setText(QString::fromStdString(visionConfig.cameraIp));
        ui->lineEdit_cameraPort->setText(QString::number(visionConfig.cameraPort));
        ui->lineEdit_cameraMmPerPix_x->setText(QString::number(visionConfig.cameraMmPerPix_x));
        ui->lineEdit_cameraMmPerPix_y->setText(QString::number(visionConfig.cameraMmPerPix_y));
        ui->lineEdit_cameraOffset_x->setText(QString::number(visionConfig.cameraOffset_x));
        ui->lineEdit_cameraOffset_y->setText(QString::number(visionConfig.cameraOffset_y));
        ui->lineEdit_cameraOffset_r->setText(QString::number(visionConfig.cameraOffset_r));
        ui->lineEdit_cameraTimeOut->setText(QString::number(visionConfig.cameraCommunicateTimeout));
        ui->lineEdit_minObjectDistance->setText(QString::number(visionConfig.objectMininumDistance));
        ui->lineEdit_visionZoneEncoderRecorded->setText(QString::number(visionConfig.visionZoneEncoderRecorded));
        ui->lineEdit_robotZoneEncoderRecorded->setText(QString::number(visionConfig.robotZoneEncoderRecorded));
        ui->comboBox_isWorkZoneCoindence->setCurrentIndex(visionConfig.isWorkZoneCoincident);
        ui->lineEdit_visionUsercoordinateIndex->setText(QString::number(visionConfig.userCoordinateIndex));
        ui->lineEdit_trigFilterRatio->setText(QString::number(visionConfig.trigFilterRatio));
        ui->lineEdit_objectLength->setText(QString::number(visionConfig.objectLength));
}

void BeltTrackView::updateTrackConfigShow(int trackIndex, BeltTrackConfig beltConfig)
{
    ui->comboBox_currenttrackIndex->setCurrentIndex(trackIndex);
    ui->comboBox_trackFunction->setCurrentIndex(beltConfig.trackFunction);
    ui->lineEdit_encoderDeviceAlias->setText(QString::number(beltConfig.encoderDeviceAlias));
    ui->lineEdit_encoderIndexOnDevice->setText(QString::number(beltConfig.encoderIndexOnDevice));
    if(1==beltConfig.encoderDirection)
    {
            ui->comboBox_encoderDirection->setCurrentIndex(0);
    }
    else if(-1==beltConfig.encoderDirection)
    {
            ui->comboBox_encoderDirection->setCurrentIndex(1);
    }
    else
    {
        ui->comboBox_encoderDirection->clear();
    }
    ui->lineEdit_trackAcceleration->setText(QString::number(beltConfig.trackTransition_startDistance));
    ui->lineEdit_trackAcceleration_end->setText(QString::number(beltConfig.trackTransition_endDistance));
    ui->lineEdit_validTrackStartPoint_fromA->setText(QString::number(beltConfig.validTrackStartPoint_fromA));
    ui->lineEdit_validTrackEndPoint_fromA->setText(QString::number(beltConfig.validTrackEndPoint_fromA));
    ui->lineEdit_encoderValue_referenceAPoint->setText(QString::number(beltConfig.encoderValue_referenceAPoint));
    ui->lineEdit_encoderValue_referenceBPoint->setText(QString::number(beltConfig.encoderValue_referenceBPoint));
    ui->lineEdit_xValue_referenceAPoint->setText(QString::number(beltConfig.xValue_referenceAPoint));
    ui->lineEdit_xValue_referenceBPoint->setText(QString::number(beltConfig.xValue_referenceBPoint));
    ui->lineEdit_yValue_referenceAPoint->setText(QString::number(beltConfig.yValue_referenceAPoint));
    ui->lineEdit_yValue_referenceBPoint->setText(QString::number(beltConfig.yValue_referenceBPoint));
    ui->lineEdit_trackPointOffset_x->setText(QString::number(beltConfig.trackPointOffset_x));
    ui->lineEdit_trackPointOffset_y->setText(QString::number(beltConfig.trackPointOffset_y));
    ui->lineEdit_trackPointOffset_z->setText(QString::number(beltConfig.trackPointOffset_z));
    ui->lineEdit_objectMininumDistance->setText(QString::number(beltConfig.objectMininumDistance));
    ui->lineEdit_startSprayPoint_fromA->setText(QString::number(beltConfig.startSprayPoint_fromA));
    ui->lineEdit_workpieceDistance->setText(QString::number(beltConfig.workpieceDistance));
    ui->lineEdit_stopDistance->setText(QString::number(beltConfig.stopDistance));
    ui->lineEdit_beltVelocity->setText(QString::number(beltConfig.beltVelocity));
    ui->lineEdit_trackSpeedAdjust->setText(QString::number(beltConfig.trackSpeedAdjust));
    ui->lineEdit_beltTransimissionAdjust->setText(QString::number(beltConfig.beltTransimissionAdjust));
    ui->lineEdit_beltTransimission->setText(QString::number(beltConfig.beltTransimission,'f',10));
    ui->comboBox_encoderDeviceType->setCurrentIndex(beltConfig.encoderDeviceType);
    ui->lineEdit_doIndexOnDevice->setText(QString::number(beltConfig.doIndexOnDevice+1));

}

void BeltTrackView::getTrackConfig_Info()
{
    beltConfigList.clear();
    communicateNormalThread->getBeltTrackConfigList(beltConfigList);

    ui->comboBox_currenttrackIndex->clear();
    for(int i=0;i<beltConfigList.size();i++)
    {
        ui->comboBox_currenttrackIndex->addItem(QString::number(i));
    }

    if(beltConfigList.size()>0)
    {
           updateTrackConfigShow(0,beltConfigList[0]);
           QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("跟踪配置获取成功"));
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("跟踪配置为空"));
        return ;
    }


}

void BeltTrackView::on_pushButton_changeTrackConfig_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否修改跟踪配置！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    int id,returnFlag;
    returnFlag=-1;
    BeltTrackConfig beltConfig;

    id= ui->comboBox_currenttrackIndex->currentIndex();

    beltConfig.trackFunction=ui->comboBox_trackFunction->currentIndex();
    beltConfig.encoderDeviceAlias=ui->lineEdit_encoderDeviceAlias->text().toInt();
    beltConfig.encoderIndexOnDevice=ui->lineEdit_encoderIndexOnDevice->text().toInt();
    if(0==ui->comboBox_encoderDirection->currentIndex())
    {
            beltConfig.encoderDirection=1;
    }
    else
    {
        beltConfig.encoderDirection=-1;
    }
    beltConfig.trackTransition_startDistance=ui->lineEdit_trackAcceleration->text().toDouble();
    beltConfig.trackTransition_endDistance=ui->lineEdit_trackAcceleration_end->text().toDouble();
    beltConfig.validTrackStartPoint_fromA=ui->lineEdit_validTrackStartPoint_fromA->text().toDouble();
    beltConfig.validTrackEndPoint_fromA=ui->lineEdit_validTrackEndPoint_fromA->text().toDouble();
    beltConfig.encoderValue_referenceAPoint=ui->lineEdit_encoderValue_referenceAPoint->text().toDouble();
    beltConfig.encoderValue_referenceBPoint=ui->lineEdit_encoderValue_referenceBPoint->text().toDouble();
    beltConfig.xValue_referenceAPoint=ui->lineEdit_xValue_referenceAPoint->text().toDouble();
    beltConfig.xValue_referenceBPoint=ui->lineEdit_xValue_referenceBPoint->text().toDouble();
    beltConfig.yValue_referenceAPoint=ui->lineEdit_yValue_referenceAPoint->text().toDouble();
    beltConfig.yValue_referenceBPoint=ui->lineEdit_yValue_referenceBPoint->text().toDouble();
    beltConfig.trackPointOffset_x=ui->lineEdit_trackPointOffset_x->text().toDouble();
    beltConfig.trackPointOffset_y=ui->lineEdit_trackPointOffset_y->text().toDouble();
    beltConfig.trackPointOffset_z=ui->lineEdit_trackPointOffset_z->text().toDouble();
    beltConfig.objectMininumDistance=ui->lineEdit_objectMininumDistance->text().toDouble();
    beltConfig.startSprayPoint_fromA=ui->lineEdit_startSprayPoint_fromA->text().toDouble();
    beltConfig.workpieceDistance=ui->lineEdit_workpieceDistance->text().toDouble();
    beltConfig.stopDistance=ui->lineEdit_stopDistance->text().toDouble();
    beltConfig.beltVelocity=ui->lineEdit_beltVelocity->text().toDouble();
    beltConfig.trackSpeedAdjust=ui->lineEdit_trackSpeedAdjust->text().toDouble();
    beltConfig.beltTransimissionAdjust=ui->lineEdit_beltTransimissionAdjust->text().toDouble();
    beltConfig.beltTransimission=ui->lineEdit_beltTransimission->text().toDouble();
    beltConfig.encoderDeviceType=ui->comboBox_encoderDeviceType->currentIndex();
    beltConfig.doIndexOnDevice=ui->lineEdit_doIndexOnDevice->text().toInt()-1;


    communicateNormalThread->changeBeltTrackConfig(id,beltConfig,returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("跟踪配置修改成功"));
    }
    else
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("跟踪配置修改失败"));
    }
}

void BeltTrackView::on_comboBox_currenttrackIndex_currentIndexChanged(int index)
{

}

int BeltTrackView::displayVisionTrackDebugInfo()
{
    VisionTrackDebugInfo visionInfoOut;
    communicateNormalThread->getVisionTrackDebugInfo(ui->comboBox_trackVisionIndex->currentIndex(),visionInfoOut);
    ui->lineEdit_visionTrackReceive_objectCount->setText(QString::number(visionInfoOut.cameraData.pointCount));
    ui->lineEdit_visionTrackReceive_x->setText(QString::number(visionInfoOut.cameraData.x,'f',3));
    ui->lineEdit_visionTrackReceive_y->setText(QString::number(visionInfoOut.cameraData.y,'f',3));
    ui->lineEdit_visionTrackReceive_r->setText(QString::number(visionInfoOut.cameraData.r,'f',3));
    ui->lineEdit_trigValue_offset->setText(QString::number(visionInfoOut.trigTimeOrDistance_offset,'f',3));
    ui->lineEdit_visionInfo_noresponsecount->setText(QString::number(visionInfoOut.noResponseCount));
    ui->lineEdit_visioninfo_responseDifferenceCount->setText(QString::number(visionInfoOut.responseDifference));
    QString connectInfo;
    if(visionInfoOut.isConnectedStatus)
    {
        connectInfo="已经成功连接";
    }
    else
    {
        connectInfo="失败：错误码";
        connectInfo+=QString::number(visionInfoOut.socketErrorCode);
    }
    ui->lineEdit_visionTrack_cameraStatus->setText(connectInfo);
    ui->lineEdit_visionObjectCount->setText(QString::number(visionInfoOut.objectCount));
    return 1;
}

int BeltTrackView::displayBeltTrackDebugInfo()
{
    BeltTrackDebugInfo beltTrackInfoOut;
    communicateNormalThread->getBeltTrackDebugInfo(ui->comboBox_trackBeltIndex->currentIndex(),beltTrackInfoOut);
//    {
//        model_beltTrackDebugInfo->clear();
//        return 0;
//    }

    model_beltTrackDebugInfo->clear();
    //定义item
    QStandardItem* item = 0;

    item = new QStandardItem("objectSize");
    model_beltTrackDebugInfo->setItem(0,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.objectSize));
    model_beltTrackDebugInfo->setItem(0,1,item);

    item = new QStandardItem("okSize");
    model_beltTrackDebugInfo->setItem(1,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.okSize));
    model_beltTrackDebugInfo->setItem(1,1,item);

    item = new QStandardItem("MovedLength");
    model_beltTrackDebugInfo->setItem(2,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.currentMovedLength,'f',4));
    model_beltTrackDebugInfo->setItem(2,1,item);

    item = new QStandardItem("movingLength");
    model_beltTrackDebugInfo->setItem(3,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.movingLength,'f',4));
    model_beltTrackDebugInfo->setItem(3,1,item);



    item = new QStandardItem("isTracking");
    model_beltTrackDebugInfo->setItem(4,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.isTracking));
    model_beltTrackDebugInfo->setItem(4,1,item);


    item = new QStandardItem("isMoveValid");
    model_beltTrackDebugInfo->setItem(5,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.isMoveValidOut));
    model_beltTrackDebugInfo->setItem(5,1,item);

    item = new QStandardItem("poseFeedback");
    model_beltTrackDebugInfo->setItem(6,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.poseFeedback,'f',1));
    model_beltTrackDebugInfo->setItem(6,1,item);

    item = new QStandardItem("poseCommand");
    model_beltTrackDebugInfo->setItem(7,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.poseCommandOut,'f',1));
    model_beltTrackDebugInfo->setItem(7,1,item);

    item = new QStandardItem("acceleration");
    model_beltTrackDebugInfo->setItem(8,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.acceleration,'f',1));
    model_beltTrackDebugInfo->setItem(8,1,item);

    item = new QStandardItem("holdPose");
    model_beltTrackDebugInfo->setItem(9,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.holdPose,'f',1));
    model_beltTrackDebugInfo->setItem(9,1,item);

    item = new QStandardItem("moveStage");
    model_beltTrackDebugInfo->setItem(10,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.moveStage));
    model_beltTrackDebugInfo->setItem(10,1,item);

    item = new QStandardItem("beltVelocity");
    model_beltTrackDebugInfo->setItem(11,0,item);
    item = new QStandardItem(QString::number(beltTrackInfoOut.beltVelocity,'f',4));
    model_beltTrackDebugInfo->setItem(11,1,item);


    ui->tableView_beltTrackDebugInfo->setModel(model_beltTrackDebugInfo);
    ui->tableView_beltTrackDebugInfo->resizeColumnToContents(0);
    ui->tableView_beltTrackDebugInfo->resizeColumnToContents(1);
    ui->tableView_beltTrackDebugInfo->resizeColumnToContents(2);
    ui->tableView_beltTrackDebugInfo->show();



}


int BeltTrackView::displayEncoderReceiver(EncoderReceiverData infoIn)
{
    model_encoderReceiverInfo->clear();
    //定义item
    QStandardItem* item = 0;

    item = new QStandardItem("doOutput");
    model_encoderReceiverInfo->setItem(0,0,item);
    item = new QStandardItem(QString::number(infoIn.doOutput));
    model_encoderReceiverInfo->setItem(0,1,item);

    item = new QStandardItem("Encoder0_Val");
    model_encoderReceiverInfo->setItem(1,0,item);
    item = new QStandardItem(QString::number(infoIn.Encoder0_Val));
    model_encoderReceiverInfo->setItem(1,1,item);

    item = new QStandardItem("Encoder1_Val");
    model_encoderReceiverInfo->setItem(2,0,item);
    item = new QStandardItem(QString::number(infoIn.Encoder1_Val));
    model_encoderReceiverInfo->setItem(2,1,item);

    item = new QStandardItem("Encoder2_Val");
    model_encoderReceiverInfo->setItem(3,0,item);
    item = new QStandardItem(QString::number(infoIn.Encoder2_Val));
    model_encoderReceiverInfo->setItem(3,1,item);

    item = new QStandardItem("HighSpeed_IN0");
    model_encoderReceiverInfo->setItem(4,0,item);
    item = new QStandardItem(QString::number(infoIn.HighSpeed_IN0));
    model_encoderReceiverInfo->setItem(4,1,item);

    item = new QStandardItem("Ltc0_Finished");
    model_encoderReceiverInfo->setItem(5,0,item);
    item = new QStandardItem(QString::number(infoIn.Ltc0_Finished));
    model_encoderReceiverInfo->setItem(5,1,item);

    item = new QStandardItem("Ltc0_Encoder0Val");
    model_encoderReceiverInfo->setItem(6,0,item);
    item = new QStandardItem(QString::number(infoIn.Ltc0_Encoder0Val));
    model_encoderReceiverInfo->setItem(6,1,item);

    item = new QStandardItem("Ltc0_Encoder1Val");
    model_encoderReceiverInfo->setItem(7,0,item);
    item = new QStandardItem(QString::number(infoIn.Ltc0_Encoder1Val));
    model_encoderReceiverInfo->setItem(7,1,item);

    item = new QStandardItem("Ltc0_Encoder2Val");
    model_encoderReceiverInfo->setItem(8,0,item);
    item = new QStandardItem(QString::number(infoIn.Ltc0_Encoder2Val));
    model_encoderReceiverInfo->setItem(8,1,item);

    item = new QStandardItem("alias");
    model_encoderReceiverInfo->setItem(9,0,item);
    item = new QStandardItem(QString::number(infoIn.deviceAlias));
    model_encoderReceiverInfo->setItem(9,1,item);




    ui->tableView_encoderReceiverData->setModel(model_encoderReceiverInfo);
    ui->tableView_encoderReceiverData->resizeColumnToContents(0);
    ui->tableView_encoderReceiverData->resizeColumnToContents(1);
    ui->tableView_encoderReceiverData->resizeColumnToContents(2);
    ui->tableView_encoderReceiverData->show();
}

int BeltTrackView::getEncoderValue(int aliasIn, int encoderIndexIn,int &encoderValueOut)
{
    std::vector<struct EncoderReceiverData> dataListReturn;
    communicateNormalThread->getEncoderReciverDataList(dataListReturn);

    for(int i=0;i<dataListReturn.size();i++)
    {
        if(aliasIn==dataListReturn[i].deviceAlias)
        {
            switch (encoderIndexIn) {
            case 0:
                encoderValueOut=dataListReturn[i].Encoder0_Val;
                return 1;
                break;
            case 1:
                encoderValueOut=dataListReturn[i].Encoder1_Val;
                return 1;
                break;
            case 2:
                encoderValueOut=dataListReturn[i].Encoder2_Val;
                return 1;
                break;
            default:
                return 0;
                break;
            }

        }
    }
    return -1;
}




void BeltTrackView::on_pushButton_recordAPointValue_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否记录A点！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }
    //坐标系检查
    int currentBeltTrackIndex=ui->comboBox_currenttrackIndex->currentIndex();
    int visionUserCoordinateIndex=-1;
    for(int i=0;i<visionConfigList.size();i++)
    {
        if(currentBeltTrackIndex==visionConfigList[i].beltTrackIndex)
        {
            visionUserCoordinateIndex=visionConfigList[i].userCoordinateIndex;
        }
    }
    if(globalData->globalDefaultUserNum!=visionUserCoordinateIndex)
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QString("当前激活的用户坐标系号(%1)非视觉使用的用户坐标系号(%2)！")
                              .arg(globalData->globalDefaultUserNum).arg(visionUserCoordinateIndex));
        return ;
    }

    int tmpAlias=ui->lineEdit_encoderDeviceAlias->text().toInt();
    int tmpEncoderValueOut=0;
    int tmpKey=1;

    //跟踪
    tmpKey=communicateNormalThread->getEncoderValue_belt(currentBeltTrackIndex,tmpEncoderValueOut);

    if(1==tmpKey)
    {
        ui->lineEdit_encoderValue_referenceAPoint->setText(QString::number(tmpEncoderValueOut));
        double tmpX,tmpY;
        PointPro currentPositionDecartOut;
        globalData->getCurrentArmPosition_decart(currentPositionDecartOut);

        if(6==currentPositionDecartOut.positionValue.size())
        {
           tmpX=currentPositionDecartOut.positionValue[0];
           tmpY=currentPositionDecartOut.positionValue[1];
        }
        ui->lineEdit_xValue_referenceAPoint->setText(QString::number(tmpX));
        ui->lineEdit_yValue_referenceAPoint->setText(QString::number(tmpY));
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("A点记录成功"));
    }
    else if(0==tmpKey)
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("编码器接索引超范围"));
        return ;
    }
    else
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("编码器接收模块别名不存在"));
        return ;
    }





}

void BeltTrackView::on_pushButton_recordBPoint_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否记录B点！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }

    //坐标系检查
    int currentBeltTrackIndex=ui->comboBox_currenttrackIndex->currentIndex();
    int visionUserCoordinateIndex=-1;
    for(int i=0;i<visionConfigList.size();i++)
    {
        if(currentBeltTrackIndex==visionConfigList[i].beltTrackIndex)
        {
            visionUserCoordinateIndex=visionConfigList[i].userCoordinateIndex;
        }
    }
    if(globalData->globalDefaultUserNum!=visionUserCoordinateIndex)
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QString("当前激活的用户坐标系号(%1)非视觉使用的用户坐标系号(%2)！")
                              .arg(globalData->globalDefaultUserNum).arg(visionUserCoordinateIndex));
        return ;
    }

    int tmpAlias=ui->lineEdit_encoderDeviceAlias->text().toInt();
    int tmpEncoderValueOut=0;
    int tmpKey=1;

    //跟踪
    tmpKey=communicateNormalThread->getEncoderValue_belt(currentBeltTrackIndex,tmpEncoderValueOut);
    if(1==tmpKey)
    {
        ui->lineEdit_encoderValue_referenceBPoint->setText(QString::number(tmpEncoderValueOut));
        double tmpX,tmpY;
        PointPro currentPositionDecartOut;
        globalData->getCurrentArmPosition_decart(currentPositionDecartOut);
        if(6==currentPositionDecartOut.positionValue.size())
        {
           tmpX=currentPositionDecartOut.positionValue[0];
           tmpY=currentPositionDecartOut.positionValue[1];
        }
        ui->lineEdit_xValue_referenceBPoint->setText(QString::number(tmpX));
        ui->lineEdit_yValue_referenceBPoint->setText(QString::number(tmpY));
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("B点记录成功"));

        on_pushButton_changeTrackConfig_clicked();
    }
    else if(0==tmpKey)
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("编码器接索引超范围"));
        return ;
    }
    else
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("编码器接收模块别名不存在"));
        return ;
    }



}

void BeltTrackView::timeOutSlot()
{
    if(ui->checkBox_getTrackInfo_vision->isChecked())
    {
           displayVisionTrackDebugInfo();
    }
    if(ui->checkBox_getTrackInfo_belt->isChecked())
    {
            displayBeltTrackDebugInfo();
    }
    if(ui->checkBox_updateEncoderValue->isChecked())
    {
        std::vector<struct EncoderReceiverData> dataListReturn;
        communicateNormalThread->getEncoderReciverDataList(dataListReturn);
        if(ui->comboBox_currentEncoderReceiver->currentIndex()<dataListReturn.size())
        {
            displayEncoderReceiver(dataListReturn[ui->comboBox_currentEncoderReceiver->currentIndex()]);

        }
        else
        {
            ui->checkBox_updateEncoderValue->setChecked(false);
            QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("选中的编码器接收模块不存在"));
        }

    }

}

void BeltTrackView::on_comboBox_visionIndex_activated(int index)
{
    visionConfigList.clear();
    communicateNormalThread->getVisionTrackConfigList(visionConfigList);

    ui->comboBox_visionIndex->clear();
    for(int i=0;i<visionConfigList.size();i++)
    {
        ui->comboBox_visionIndex->addItem(QString::number(i));
    }


    if(visionConfigList.size()>index)
    {
           updateVisionConfigShow(index,visionConfigList[index]);
           QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("视觉配置获取成功"));
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("视觉配置为空２"));
        return ;
    }
}

void BeltTrackView::on_comboBox_currenttrackIndex_activated(int index)
{
    beltConfigList.clear();
    communicateNormalThread->getBeltTrackConfigList(beltConfigList);

    ui->comboBox_currenttrackIndex->clear();
    for(int i=0;i<beltConfigList.size();i++)
    {
        ui->comboBox_currenttrackIndex->addItem(QString::number(i));
    }


    if(beltConfigList.size()>index)
    {
           updateTrackConfigShow(index,beltConfigList[index]);
           QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("跟踪配置获取成功"));
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("跟踪配置为空３"));
        return ;
    }
}

void BeltTrackView::on_pushButton_forceTrigCamera_clicked()
{
    int visionIndexIn=ui->comboBox_trackVisionIndex->currentIndex();
    int returnFlag;
    qDebug()<<"forceTrigCamera 0";
    communicateNormalThread->forceTrigCamera( visionIndexIn,false,returnFlag);
    if(4!=visionConfigList[visionIndexIn].cameraTrigMethod)
    {
        usleep(100000);
        qDebug()<<"forceTrigCamera 1";
        communicateNormalThread->forceTrigCamera( visionIndexIn,true,returnFlag);
        usleep(100000);
        qDebug()<<"forceTrigCamera 2";
        communicateNormalThread->forceTrigCamera( visionIndexIn,false,returnFlag);
    }

    if(1==returnFlag)
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("触发拍照成功"));
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("触发拍照失败"));
    }
}

void BeltTrackView::on_pushButton_recordCameraPoint_clicked()
{
    QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("请确认已经配置好视觉关联的跟踪号，以及跟踪的具体配置"));
    int visionIndexIn=ui->comboBox_visionIndex->currentIndex();

    VisionTrackDebugInfo visionInfoOut;
    communicateNormalThread->getVisionTrackDebugInfo(visionIndexIn,visionInfoOut);
    int returnFlag;
    if(false==visionInfoOut.isConnectedStatus)
    {
        qDebug()<<"tryconnect camera";
        communicateNormalThread->forceConnectCamera( visionIndexIn,returnFlag);
        if(1==returnFlag)
        {
            QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("相机连接成功"));
        }
        else
        {
            QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("相机连接失败"));
            return ;
        }
    }


    communicateNormalThread->forceTrigCamera( visionIndexIn,false,returnFlag);
    usleep(100000);
    communicateNormalThread->forceTrigCamera( visionIndexIn,true,returnFlag);
    usleep(100000);
    communicateNormalThread->forceTrigCamera( visionIndexIn,false,returnFlag);

    if(1!=returnFlag)
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("触发拍照失败"));
        return ;
    }
    sleep(1);

    communicateNormalThread->getVisionTrackDebugInfo(visionIndexIn,visionInfoOut);
    if(1!=visionInfoOut.cameraData.pointCount)
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("未检测到摸板物体"));
        return ;
    }
    ui->lineEdit_xValue_camera->setText(QString::number(visionInfoOut.cameraData.x));
    ui->lineEdit_yValue_camera->setText(QString::number(visionInfoOut.cameraData.y));
    int tmpEncoderValueOut=0;
    int tmpKey=1;
    //跟踪
    if(1==ui->comboBox_isTrack->currentIndex())
    {
        tmpKey=communicateNormalThread->getEncoderValue_vision(visionIndexIn,tmpEncoderValueOut);
    }

    if(1==tmpKey)
    {
        ui->lineEdit_encoderValue_camera->setText(QString::number(tmpEncoderValueOut));

        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("第１点记录成功"));
    }
    else
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("编码器值获取失败"));
        return ;
    }

}

void BeltTrackView::on_pushButton_recordARobotPoint_clicked()
{
    QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("请确认已经配置好视觉关联的跟踪号，以及跟踪的具体配置"));
    int visionIndexIn=ui->comboBox_visionIndex->currentIndex();
    int tmpEncoderValueOut=0;
    int tmpKey=1;
    //跟踪
    if(1==ui->comboBox_isTrack->currentIndex())
    {
        tmpKey=communicateNormalThread->getEncoderValue_vision(visionIndexIn,tmpEncoderValueOut);
    }

    if(1==tmpKey)
    {
        ui->lineEdit_encoderValue_robotPoint->setText(QString::number(tmpEncoderValueOut));
        double tmpX,tmpY;
        PointPro currentPositionDecartOut;
        globalData->getCurrentArmPosition_decart(currentPositionDecartOut);
//        globalData->lock();
        if(6==currentPositionDecartOut.positionValue.size())
        {
           tmpX=currentPositionDecartOut.positionValue[0];
           tmpY=currentPositionDecartOut.positionValue[1];
        }
//        globalData->unlock();
        ui->lineEdit_xValueRobotPoint->setText(QString::number(tmpX));
        ui->lineEdit_yValueRobotPoint->setText(QString::number(tmpY));
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("第2点记录成功"));
    }
    else
    {
        QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("编码器值获取失败"));
        return ;
    }


}

void BeltTrackView::on_pushButton_calculateCameraOffset_clicked()
{
    double tmpXbias;
    double tmpYBias;
    //跟踪
    if(1==ui->comboBox_isTrack->currentIndex())
    {
        QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("请确认已经配置好视觉关联的跟踪号，以及跟踪的具体配置!"));
        double tmpTransmission;
        std::vector<BeltTrackConfig> beltConfig;
        communicateNormalThread->getBeltTrackConfigList(beltConfig);
        std::vector<struct VisionTrackConfig> visionConfig;
        communicateNormalThread->getVisionTrackConfigList(visionConfig);
        if(0==visionConfig.size())
        {
            QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("获取视觉配置失败"));
            return ;
        }
        int visionIndexIn=ui->comboBox_visionIndex->currentIndex();
        if(visionConfig[visionIndexIn].beltTrackIndex<0 ||visionConfig[visionIndexIn].beltTrackIndex>=beltConfig.size())
        {
            QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("找不到皮带传动比"));
            return ;
        }
        tmpTransmission=beltConfig[visionConfig[visionIndexIn].beltTrackIndex].beltTransimission+
                beltConfig[visionConfig[visionIndexIn].beltTrackIndex].beltTransimissionAdjust;
        double tmpMovedLength=1000*getActualMovedDistance(ui->lineEdit_encoderValue_camera->text().toInt()
                                                     ,ui->lineEdit_encoderValue_robotPoint->text().toInt(),
                                   beltConfig[visionConfig[visionIndexIn].beltTrackIndex].encoderDirection,tmpTransmission);

        double beltMoveOrientation=atan2(beltConfig[visionConfig[visionIndexIn].beltTrackIndex].yValue_referenceBPoint
                -beltConfig[visionConfig[visionIndexIn].beltTrackIndex].yValue_referenceAPoint,
                                  beltConfig[visionConfig[visionIndexIn].beltTrackIndex].xValue_referenceBPoint
                -beltConfig[visionConfig[visionIndexIn].beltTrackIndex].xValue_referenceAPoint);

        tmpXbias=ui->lineEdit_xValueRobotPoint->text().toDouble()-ui->lineEdit_xValue_camera->text().toDouble()
                -tmpMovedLength*cos(beltMoveOrientation);
        tmpYBias=ui->lineEdit_yValueRobotPoint->text().toDouble()-ui->lineEdit_yValue_camera->text().toDouble()
                -tmpMovedLength*sin(beltMoveOrientation);
        qDebug()<<"beltMoveOrientation"<<180*beltMoveOrientation/M_PI<<"tmpMovedLength"<<tmpMovedLength
               <<"tmpMovedLength*cos(beltMoveOrientation)"<<tmpMovedLength*cos(beltMoveOrientation)
                 <<"tmpMovedLength*sin(beltMoveOrientation)"<<tmpMovedLength*sin(beltMoveOrientation);
    }
    else
    {
        tmpXbias=ui->lineEdit_xValueRobotPoint->text().toDouble()-ui->lineEdit_xValue_camera->text().toDouble();
        tmpYBias=ui->lineEdit_yValueRobotPoint->text().toDouble()-ui->lineEdit_yValue_camera->text().toDouble();
    }



    ui->lineEdit_cameraOffset_x->setText(QString::number(tmpXbias));
    ui->lineEdit_cameraOffset_y->setText(QString::number(tmpYBias));
    ui->lineEdit_cameraOffset_r->setText(QString::number(0));
    ui->lineEdit_cameraMmPerPix_x->setText(QString::number(1));
    ui->lineEdit_cameraMmPerPix_y->setText(QString::number(1));
    QMessageBox::critical(this,QStringLiteral("提示"),QStringLiteral("计算相机偏移成功"));

    on_pushButton_changeVisionConfig_clicked();

}



#define D_2POW30 1073741824
#define D_2POW32 4294967296

double BeltTrackView::getActualMovedDistance(int oldValue, int newValue,int encoderDirection,double transmission)
{
    //允许编码器每次最大变化量2^30
    long long count=0;
    long long deltaMovePulse_long;
    long long newLeng=newValue;
    long long oldLeng=oldValue;
    long long deltaMovePulse=newLeng-oldLeng;
    if(deltaMovePulse>D_2POW30)
    {
        count--;
        qDebug()<<"BeltTrack::getActualMovedDistance int 溢出１, newValue"<<newValue<<" oldValue"<<oldValue;
    }
    else if(deltaMovePulse<-D_2POW30)
    {
        count++;
        qDebug()<<"BeltTrack::getActualMovedDistance int 溢出2, newValue"<<newValue<<" oldValue"<<oldValue;
    }
    else
    {

    }
    deltaMovePulse_long=count*D_2POW32+deltaMovePulse;
    deltaMovePulse=deltaMovePulse_long;
    if(0!=count)
    {
        qDebug()<<"count"<<count<<" deltaMovePulse_long"<<deltaMovePulse_long<<" deltaMovePulse"<<deltaMovePulse;
    }

    double tmpMovedLength=deltaMovePulse*transmission*encoderDirection;
//    qDebug()<<"tmpMovedLength"<<tmpMovedLength<<" deltaMovePulse"<<deltaMovePulse
//           <<" beltTransimission"<<beltConfig.beltTransimission<<" encoderDirection"<<beltConfig.encoderDirection;
    return tmpMovedLength;
}

void BeltTrackView::on_pushButton_connectCamera_clicked()
{
    int visionIndexIn=ui->comboBox_trackVisionIndex->currentIndex();
    int returnFlag;
    communicateNormalThread->forceConnectCamera( visionIndexIn,returnFlag);
    if(1==returnFlag)
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("相机连接成功"));
    }
    else
    {
        QMessageBox::warning(this,QStringLiteral("提示"),QStringLiteral("相机连接失败"));
    }
}
